﻿/*****************************************
 * 
 *  copyright (c) by ylshen
 * 
 *  author : ylshen (syl.lucky@gmail.com)
 *  
 *  created at : 08-05-21
 * 
 **************************************** */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using JYStockAnalyser.Model;

namespace JYStockAnalyser.DA
{
    /// <summary>
    /// 
    /// </summary>
    public class UserMapper : ORMapper
    {
        public List<User> FindAllUsers()
        {
            return null;
        }

        public User FindUserByID(int id)
        {
            return null;
        }

        public User FindUserByUserName(string userName)
        {
            string filter = String.Format(" WHERE userName = '{0}'", userName);
            return m_findUserByFilter(filter);
        }

        public void InsertUser(User user)
        {
        }

        public void UpdateUser(User user)
        {
            SqlConnection cn = CreateConnection();
            SqlCommand cmd = new SqlCommand("p_UpdateUser", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@id", user.Id));
            cmd.Parameters.Add(new SqlParameter("@username", user.UserName));
            cmd.Parameters.Add(new SqlParameter("@password", user.Password));
            cmd.Parameters.Add(new SqlParameter("@sex", user.Sex));
            cmd.Parameters.Add(new SqlParameter("@work", user.Work));
            cmd.Parameters.Add(new SqlParameter("@contractNO", user.ContractNO));
            cmd.Parameters.Add(new SqlParameter("@email", user.Email));
            if (user.UniqueNO == null)
            {
                cmd.Parameters.Add(new SqlParameter("@isBinded", false));
            }
            else
            {
                cmd.Parameters.Add(new SqlParameter("@isBinded", true));
                cmd.Parameters.Add(new SqlParameter("@cpuNO", user.UniqueNO.CPU_NO));             
            }
            
            cmd.Parameters.Add(new SqlParameter("@endDate", GetDateTimeString(user.EndDate)));

            cn.Open();
            try
            {
                cmd.ExecuteNonQuery();
            }
            catch (System.Data.SqlClient.SqlException sqlErr)
            {
                if (sqlErr.Number == 130007)
                {
                    throw new ORException(ORException.ERROR_NAME_EXISTS);
                }
                throw new ORException(ORException.ERROR_OTHER);
            }
            catch (System.Exception err)
            {
                Loger.WriteErrorLog(err.ToString());
                throw new ORException();
            }
            finally
            {
                cn.Close();
            }
        }

        public void DeleteUser(int id)
        {
        }

        public User m_findUserByFilter(string filter)
        {
            List<User> users = m_findUsersByFilter(filter);
            return users.Count == 0 ? null : users[0];
        }

        public List<User> m_findUsersByFilter(string filter)
        {
            string SQL = "SELECT id, userName, name, password, sex, [work], contractNO, email, isBinded, cpuNO, endDate FROM t_users" + filter;
            List<User> users = new List<User>();
            using (SqlDataReader reader = Excute_Reader(SQL))
            {
                while (reader.Read())
                {
                    User user = new User();
                    user.Id = GetReaderInt32Value(reader, 0);
                    user.UserName = GetReaderString(reader, 1);
                    user.Name = GetReaderString(reader, 2);
                    user.Password = GetReaderString(reader, 3);
                    user.Sex = GetReaderString(reader, 4);
                    user.Work = GetReaderString(reader, 5);
                    user.ContractNO = GetReaderString(reader, 6);
                    user.Email = GetReaderString(reader, 7);
                    bool isBinded = GetReaderBooleanValue(reader, 8);
                    if (isBinded)
                    {
                        user.UniqueNO = new ComputerUniqueNO();
                        user.UniqueNO.CPU_NO = GetReaderString(reader, 9);                
                    }

                    user.EndDate = GetReaderDateTime(reader, 10);

                    users.Add(user);
                }
            }

            return users;
        }
    }
}
